# -*- coding:utf-8 -*-
# file_name      vip_video_final.py


__author__ = 'charon'

import tkinter as tk
import webbrowser
from tkinter import ttk
from tkinter.messagebox import showwarning
from tkinter import Tk, Label, StringVar, Radiobutton, Entry, Button, Frame
from tkinter.ttk import Combobox
from PIL import Image, ImageTk
import requests
from bs4 import BeautifulSoup

txt_list = []
mapping = dict()


def center_window(window, w, h):
    # 获取屏幕 宽、高
    ws = window.winfo_screenwidth()
    hs = window.winfo_screenheight()
    # 计算 x, y 位置
    x = (ws / 2) - (w / 2)
    y = (hs / 2) - (h / 2)
    window.geometry('%dx%d+%d+%d' % (w, h, x, y))


def do_search():
    search_text = search_v.get().strip()
    query_site = site_v.get().strip()
    query = query_v.get().strip()
    op_value = op_v.get().strip()
    if op_value == 'link':
        full_url = f'{query_site}?url={query}'
        cbox.set(full_url)
        print(f'full_url')
    else:
        print(f'query_site={query_site}, search_text={search_text}')
        if search_text == '1':
            fetch_teleplay_url(query, query_site)
        elif search_text == '2':
            fetch_movie_url(query, query_site)
        else:
            # 综艺
            fetch_tv_show_url(query, query_site)
        cbox['value'] = txt_list
        if len(txt_list) > 0:
            cbox.set(txt_list[0])
        else:
            showwarning('搜索结果', f'为找到{query}对应的电视剧,请尝试使用链接解析')


def play_video():
    if op_v.get().strip() == 'link':
        select_items = cbox.get()
        webbrowser.open(select_items)
    else:
        select_items = cbox.get()
        print(f'select_items={select_items}')
        play_url = mapping[select_items]
        webbrowser.open(play_url)


def fetch_teleplay_url(query, line=None):
    global txt_list
    if len(txt_list) > 0:
        txt_list = []
        mapping.clear()
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                      'Chrome/103.0.0.0 Safari/537.36',
        'referer': 'https://v.qq.com/'
    }
    url = f'https://v.qq.com/x/search/?q={query}'
    html_content = requests.get(url, headers=headers, verify=False).content.decode('utf-8')
    parser = BeautifulSoup(html_content, 'html.parser')

    root_div = parser.find('div', attrs={'class': 'result_episode_list'})
    if root_div is not None:
        link_urls = root_div.find_all('a', attrs={'dt-eid': 'poster'})
        expand_link = root_div.find('a', attrs={'dt-eid': 'expand_btn'})
        movie_name = link_urls[0].get('dt-params').split('&')[0].split('=')[-1]

        def handler_hide_url(ele_link):

            v_id = None
            site = None
            page_num = None
            detail = ele_link.get('dt-params')
            for p in detail.split('&'):
                if 'cid' in p:
                    v_id = p.split('=')[-1]
                if 'site_id' in p:
                    site = p.split('=')[-1]
                if 'pg_num' in p:
                    page_num = p.split('=')[-1]

            cookies = {
                '_pcmgr_localtk': '9QgVb6(74L',
                '_qpsvr_localtk': 'O)6kvwidDd',
                'pgv_info': 'ssid=s3912331901',
                'pgv_pvid': '4948984288',
                'tvfe_boss_uuid': '78adf163dd9d6537',
                'video_platform': '2',
                'video_guid': '991b6533a95a6b2b',
            }

            headers = {
                'authority': 'pbaccess.video.qq.com',
                'origin': 'https://v.qq.com',
                'referer': 'https://v.qq.com/',
                'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
            }
            params = {
                'pageNum': page_num,
                'id': v_id,
                'dataType': '2',
                'pageContext': 'need_async=true',
                'scene': '2',
                'platform': '2',
                'appId': '10718',
                'site': site,
                'vappid': '34382579',
                'vsecret': 'e496b057758aeb04b3a2d623c952a1c47e04ffb0a01e19cf',
            }
            detail_url = 'https://pbaccess.video.qq.com/trpc.videosearch.search_cgi.http/load_playsource_list_info'
            res_json = requests.get(
                detail_url,
                params=params,
                cookies=cookies,
                headers=headers,
            ).json()
            json_data = res_json['data']['normalList']['itemList'][0]['videoInfo']['firstBlockSites'][0][
                'episodeInfoList']
            return json_data

        if expand_link is not None:
            # 包含隐藏的部分
            movie_detail = handler_hide_url(expand_link)
        else:
            movie_detail = []
            for index, url in enumerate(link_urls):
                movie_obj = {
                    'url': url.get('href'),
                    'title': index + 1
                }
                movie_detail.append(movie_obj)

        for item in movie_detail:
            play_url = item['url']
            current_index = item['title']
            full_url = f'{line}?url={play_url}'
            title = '【' + movie_name + '】' + f'第{current_index}集'
            mapping[title] = full_url
            txt_list.append(title)

    else:
        print(f'没找到对应的电视剧')


def fetch_movie_url(query, line=None):
    global txt_list
    if len(txt_list) > 0:
        txt_list = []
        mapping.clear()
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
        'referer': 'https://v.qq.com/'
    }
    url = f'https://v.qq.com/x/search/?q={query}'
    html_content = requests.get(url, headers=headers, verify=False).content.decode('utf-8')
    parser = BeautifulSoup(html_content, 'html.parser')
    root_div = parser.find('div', attrs={'class': 'result_btn_line'})
    if root_div is not None:
        link_item = root_div.find('a')
        play_url = link_item.get('href')
        detail = link_item.get('dt-params')
        movie_name = detail.split('&')[0].split('=')[-1]
        full_url = f'{line}?url={play_url}'
        mapping[movie_name] = full_url
        txt_list.append(movie_name)

    else:
        print(f'未找到{query}对应的电影')


def parse_and_open_video():
    url = link_v.get().strip()
    if url:
        if validate_url(url):
            open_video(url)
        else:
            tk.messagebox.showinfo("提示", "请输入正确的视频链接")
    else:
        tk.messagebox.showwarning("警告", "请输入视频链接")


def open_video(url):
    # 确保线路和URL都有值
    if not url or not site_v.get():
        tk.messagebox.showerror("错误", "请选择线路并输入视频链接")
        return

    try:
        # 构建完整URL
        full_url = site_v.get() + '?url=' + url

        # 使用webbrowser打开URL
        webbrowser.open(full_url)

        # 打印日志信息
        print("视频已打开")
    except Exception as e:
        # 异常处理：线路URL无效或无法访问
        tk.messagebox.showerror("错误", f"无法打开视频: {str(e)}")


def open_video(url):
    # 通过线路来解析播放视频
    url = site_v.get() + '?url=' + url

    webbrowser.open(url)
    print("视频已打开")


def validate_url(url):
    # 验证url是否正确
    return True


def fetch_tv_show_url(query, line=None):
    global txt_list
    if len(txt_list) > 0:
        txt_list = []
        mapping.clear()
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                      'Chrome/103.0.0.0 Safari/537.36',
        'referer': 'https://v.qq.com/'
    }
    url = f'https://v.qq.com/x/search/?q={query}'
    html_content = requests.get(url, headers=headers, verify=False).content.decode('utf-8')
    parser = BeautifulSoup(html_content, 'html.parser')

    root_div = parser.find('div', attrs={'class': 'result_link_list'})
    movie_detail = []
    if root_div is not None:
        link_urls = root_div.find_all('a', attrs={'dt-eid': 'poster'})
        expand_link = root_div.find('a', attrs={'dt-eid': 'expand_btn'})
        # 先加载页面上已经存在的
        for url in link_urls:
            movie_obj = {
                'url': url.get('href'),
                'title': url.get('title')
            }
            movie_detail.append(movie_obj)

        def handler_hide_url(ele_link, index):

            v_id = None
            site = None
            detail = ele_link.get('dt-params')
            for p in detail.split('&'):
                if 'cid' in p:
                    v_id = p.split('=')[-1]
                if 'site_id' in p:
                    site = p.split('=')[-1]

            cookies = {
                'tvfe_boss_uuid': '24c539ba5cc5db67',
                'pgv_pvid': '4341653816',
                'video_platform': '2',
                'video_guid': 'f721a570554a868a',
                'pgv_info': 'ssid=s8973242302',
                'vversion_name': '8.2.95',
                'video_omgid': 'f721a570554a868a',
            }

            headers = {
                'authority': 'pbaccess.video.qq.com',
                'origin': 'https://v.qq.com',
                'referer': 'https://v.qq.com/',
                'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36',
            }

            params = {
                'pageNum': index,
                'id': v_id,
                'dataType': '2',
                'pageContext': 'need_async=true&offset_begin=5',
                'scene': '3',
                'platform': '2',
                'appId': '10718',
                'site': site,
                'vappid': '34382579',
                'vsecret': 'e496b057758aeb04b3a2d623c952a1c47e04ffb0a01e19cf',
                'g_tk': '',
                'g_vstk': '',
                'g_actk': '',
            }

            detail_url = 'https://pbaccess.video.qq.com/trpc.videosearch.search_cgi.http/load_playsource_list_info'
            res_json = requests.get(
                detail_url,
                params=params,
                cookies=cookies,
                headers=headers,
            ).json()
            json_data = res_json['data']['normalList']['itemList'][0]['videoInfo']['firstBlockSites'][0][
                'episodeInfoList']
            return json_data

        if expand_link is not None:
            load_all = False
            current_index = 0
            while not load_all:
                # 包含隐藏的部分
                result_data = handler_hide_url(expand_link, current_index)
                current_index += 1
                last_data = result_data[-1]
                for movie in result_data:
                    if str(movie.get('displayType')) == '0':
                        movie_obj = {
                            'url': movie.get('url'),
                            'title': movie.get('title')
                        }
                        movie_detail.append(movie_obj)
                if str(last_data.get('displayType')) == '0':
                    load_all = True
        for item in movie_detail:
            play_url = item['url']
            title = item['title']
            full_url = f'{line}?url={play_url}'
            mapping[title] = full_url
            txt_list.append(title)
    else:
        print(f'没找到对应的内容')


def show_help():
    help_text = """
    欢迎使用VIP视频解析器！
    搜索的视频点右边播放即可
    通过视频链接解析：点击解析在线播放
    切记！测试版本后续会整改
    功能说明：
    1. 选择解析通道：从提供的线路中选择一个用于视频解析，目前版本所有线路设置一样，所有默认全选状态。
    2. 选择搜索类别：选择电视剧、电影或综艺。
    3. 输入视频名称：输入你想要搜索的视频名称。
    4. 点击搜索：开始搜索视频。
    5. 选择搜索结果：从下拉列表中选择搜索结果。
    6. 点击播放：播放所选视频。不是点击解析在线播放。
    7. 如果搜索不到视频，可以去各大网站如腾讯，爱奇艺，优酷等将视频网址复制到视频链接中。
    8. 复制完后点击解析在线播放。

    注意事项：
    - 搜索时尽量输入完整的视频名称和第几部。
    - 确保网络连接良好。
    - 搜索结果可能受网站内容更新影响。
    - 本程序仅供学习交流使用，请勿用于商业目的。

    如果遇到问题，请检查输入是否正确或联系开发者wx：Wwy35779。
    """
    tk.messagebox.showinfo("帮助", help_text)


def exit_program():
    window.destroy()


# 在界面中添加帮助按钮


window = tk.Tk()
# 添加界面图片
window.title('视频解析')
window.geometry('500x300')
import os
import sys

if getattr(sys, 'frozen', False):
    # 如果应用是冻结的，我们处于包中
    basedir = sys._MEIPASS
else:
    # 我们处于源代码目录中
    basedir = os.path.dirname(os.path.abspath(__file__))

image_path = os.path.join(basedir, "./IMG_9337.JPG")
img = Image.open(image_path)

max_width = 500
help_button = tk.Button(window, text='帮助', command=show_help)
help_button.grid(row=7, column=3)
# 打开图片并调整大小
try:
    img = Image.open(image_path)
    width_percent = (max_width / float(img.size[0]))
    height_size = int((float(img.size[1]) * float(width_percent)))
    img = img.resize((max_width, height_size), Image.Resampling.LANCZOS)
    img = ImageTk.PhotoImage(img)
    label_img = Label(window, image=img)
    label_img.image = img  # keep a reference!
    label_img.grid(row=0, column=0, columnspan=4)  # place it at the top spanning all columns
except FileNotFoundError:
    print(f"Image file {image_path} not found.")

label3 = tk.Label(window, text="解析通道")
label3.grid(row=2)
site = [('线路一', 'https://jx.xmflv.com/'),
        ('线路二', 'https://jx.xmflv.com/'),
        ('线路三', 'https://jx.xmflv.com/')]

# 正确地为站点选择设置StringVar
site_v = tk.StringVar()

# 设置初始值为第一个站点
site_v.set(site[0][1])

# 使用相同的StringVar创建Radiobuttons
site_radio_01 = tk.Radiobutton(window, text=site[0][0], variable=site_v, value=site[0][1])
site_radio_02 = tk.Radiobutton(window, text=site[1][0], variable=site_v, value=site[1][1])
site_radio_03 = tk.Radiobutton(window, text=site[2][0], variable=site_v, value=site[2][1])

# 按照之前的方式网格化布局
site_radio_01.grid(row=2, column=1)
site_radio_02.grid(row=2, column=2)
site_radio_03.grid(row=2, column=3)

# ... 其他代码 ...


label3 = tk.Label(window, text="搜索类别")
label3.grid(row=3)
search_type = [('电视剧', '1'), ('电影', '2'), ('综艺', '3')]
search_v = tk.StringVar()
type_radio_01 = tk.Radiobutton(window, text=search_type[0][0], variable=search_v, value=search_type[0][1])
type_radio_01.grid(row=3, column=1)
type_radio_01.select()
type_radio_02 = tk.Radiobutton(window, text=search_type[1][0], variable=search_v, value=search_type[1][1])
type_radio_02.grid(row=3, column=2)
type_radio_03 = tk.Radiobutton(window, text=search_type[2][0], variable=search_v, value=search_type[2][1])
type_radio_03.grid(row=3, column=3)
label3 = tk.Label(window, text="搜索方式")
label3.grid(row=3)
op_type = [('搜索', 'search'), ('链接', 'link')]
label4 = tk.Label(window, text="搜索类型")
label4.grid(row=4)
op_v = tk.StringVar()
op_v.set(op_type[0][1])
op_radio_01 = tk.Radiobutton(window, text=op_type[0][0], variable=op_v, value=op_type[0][1])
op_radio_01.grid(row=4, column=1)
op_radio_01.select()
op_radio_02 = tk.Radiobutton(window, text=op_type[1][0], variable=op_v, value=op_type[1][1])
op_radio_02.grid(row=4, column=2)

center_window(window, 520, 520)
window.title("vip视频解析（作者：M1ghty_2wy,如第一次使用请打开帮助查看使用教程）")
window.resizable(0, 0)
query_label = tk.Label(window, text="视频名称")
query_label.grid(row=5)
query_v = tk.StringVar()
query_input = tk.Entry(window, textvariable=query_v)
query_input.grid(row=5, column=1)
search_btn = tk.Button(window, text='搜索', command=do_search)
search_btn.grid(row=6, column=2)
result_label = tk.Label(window, text="搜索结果")
result_label.grid(row=6)
cbox = ttk.Combobox(window, state='readonly')
cbox.grid(row=6, column=1)
play_btn = tk.Button(window, text='播放', command=play_video)
play_btn.grid(row=6, column=3)
# 添加链接输入框
link_label = tk.Label(window, text="视频链接")
link_label.grid(row=7, column=0)
link_v = tk.StringVar()
link_input = tk.Entry(window, textvariable=link_v)
link_input.grid(row=7, column=1)

buttons_frame = tk.Frame(window)
buttons_frame.grid(row=8, column=0, columnspan=4, sticky="ew")  # 使用grid布局并横跨所有列
tk.Button(buttons_frame, text="点击解析在线播放", font=("微软雅黑", 14), relief="flat",
          command=parse_and_open_video).pack(side=tk.LEFT, fill="both", expand=True)


# 在你的代码末尾附近添加以下代码
# 定义退出函数
def clear_link_input():
    link_input.delete(0, tk.END)


# 添加清空视频链接输入框的按钮
clear_link_button = tk.Button(window, text="清空链接", command=clear_link_input)
clear_link_button.grid(row=7, column=2)  # 调整位置以适应你的布局

# 添加退出按钮
exit_button = tk.Button(window, text="退出", command=exit_program)
exit_button.grid(row=9, column=3, sticky="se")  # 将按钮放置在右下角

window.mainloop()

# pyinstaller --onefile --noconsole -i t1.jpg --add-data "t1.JPG:." 视频破解.py
